#include "Color.h"
#include "stdio.h"
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "Algorithm.h"


vector<MYRECT *> piece;

vector<MYRECT *> *Tag(unsigned char *colorBuff, int width, int height, int bit) {
    for (int i = 0; i < piece.size(); i++) {
        MYRECT *sg = piece[i];
        piece.erase(piece.begin() + i);
        delete sg;
    }
    piece.clear();

    int stride = width * bit;

    MYRECT *single = NULL;


    for (int i = 3; i < height - 1; i++) {
        int start = -1;
        int end = -1;
        int C = 100;
        int widthColorID = i * stride;

        bool isBlock = false;
        for (int j = 1; j < width / 4; j++) {
            unsigned char B = colorBuff[widthColorID + j * bit + 0];
            unsigned char G = colorBuff[widthColorID + j * bit + 1];
            unsigned char R = colorBuff[widthColorID + j * bit + 2];

            if (B < C && G < C && R < C) {
                if (colorBuff[widthColorID + (j - 1) * bit + 3] != 0) {
                    if (start == -1)
                        start = j;
                    else
                        end = j;
                }
                else {
                    if (end == -1)
                        start++;
                    else
                        end++;
                }
            }
        }
        if (end > start) {
            C = 20;

            int s = start + (end - start) / 2;
            unsigned char B1 = colorBuff[((i - 1) * stride) + s * bit + 0];
            unsigned char G1 = colorBuff[((i - 1) * stride) + s * bit + 1];
            unsigned char R1 = colorBuff[((i - 1) * stride) + s * bit + 2];
            int L1 = (R1 + G1 + B1) / 3.0;

            unsigned char B2 = colorBuff[widthColorID + s * bit + 0];
            unsigned char G2 = colorBuff[widthColorID + s * bit + 1];
            unsigned char R2 = colorBuff[widthColorID + s * bit + 2];
            int L2 = (R2 + G2 + B2) / 3.0;

            unsigned char B3 = colorBuff[((i + 1) * stride) + s * bit + 0];
            unsigned char G3 = colorBuff[((i + 1) * stride) + s * bit + 1];
            unsigned char R3 = colorBuff[((i + 1) * stride) + s * bit + 2];
            int L3 = (R3 + G3 + B3) / 3.0;

            int L = (L1 + L2 + L3) / 3.0;

            if (fabs(B2 - L) > C || fabs(G2 - L) > C || fabs(R2 - L) > C) {
                //色块
                if (isBlock == false) {
                    if (!single) {
                        single = new MYRECT();
                        single->top = i;
                        single->left = s;
                        single->right = s;

                    }


                }
                isBlock = true;
                single->botton = i;

                unsigned char aa = 0;
                for (int j = s; j > start; j--) {
                    if (fabs(colorBuff[widthColorID + j * bit + 0] - B2) < 10) {
                        if (j != s && aa != 0)
                            break;
                        aa = 0;

                        if (j < single->left)
                            single->left = j;
                    }
                    else aa = 255;
                }
                aa = 0;
                for (int j = s; j < end; j++) {
                    if (fabs(colorBuff[widthColorID + j * bit + 0] - B2) < 10) {
                        if (j != s && aa != 0)
                            break;
                        aa = 0;

                        if (j > single->right)
                            single->right = j;
                    }
                    else
                        aa = 255;
                }
            }
            else {
                //间隙
                isBlock = false;
                if (single) {
                    int c = single->top + (single->botton - single->top) / 2;
                    single->B = colorBuff[c * stride + s * bit + 0];
                    single->G = colorBuff[c * stride + s * bit + 1];
                    single->R = colorBuff[c * stride + s * bit + 2];

                    float bl =
                            (single->botton - single->top) / (float) (single->right - single->left);
                    if (bl > 0.4) {
                        if (piece.size() > 0) {
                            if (single->top - piece[piece.size() - 1]->botton >
                                piece[piece.size() - 1]->botton - piece[piece.size() - 1]->top) {
                                MYRECT *patch = new MYRECT();
                                patch->top = piece[piece.size() - 1]->botton;
                                patch->left = piece[piece.size() - 1]->left;
                                patch->right = piece[piece.size() - 1]->right;
                                patch->botton = single->top;

                                int c = patch->top + (patch->botton - patch->top) / 2;
                                patch->B = colorBuff[c * stride + s * bit + 0];
                                patch->G = colorBuff[c * stride + s * bit + 1];
                                patch->R = colorBuff[c * stride + s * bit + 2];

                                piece.push_back(patch);
                            }
                        }
                        piece.push_back(single);
                    }

                    else
                        delete single;
                }
                single = NULL;
            }

        }
    }

    for (int i = 0; i < piece.size(); i++) {
        for (int j = piece[i]->top; j < piece[i]->botton; j++) {
            for (int k = piece[i]->left + 200; k < piece[i]->right + 200; k++) {
                int widthColorID = j * stride;
                colorBuff[widthColorID + k * bit + 0] = piece[i]->B;
                colorBuff[widthColorID + k * bit + 1] = piece[i]->G;
                colorBuff[widthColorID + k * bit + 2] = piece[i]->R;
            }
        }

    }
    return &piece;
}

MYRECT interception(unsigned char *colorBuff, int width, int height, int bit,
                    unsigned char **buff) {
    MYRECT rect;
    rect.left = width, rect.top = height, rect.right = 0, rect.botton = 0;
    int stride = width * bit;
    for (int i = 0; i < height; i++) {
        int widthColorID = i * stride;
        for (int j = 0; j < width; j++) {
            unsigned char B = colorBuff[widthColorID + j * bit + 0];
            unsigned char G = colorBuff[widthColorID + j * bit + 1];
            unsigned char R = colorBuff[widthColorID + j * bit + 2];
            if (bit == 4)
                colorBuff[widthColorID + j * bit + 3];


            int C = 100;
            if (B < C && G < C && R < C) {
                if (j < rect.left)
                    rect.left = j;

                if (j > rect.right)
                    rect.right = j;

                if (i < rect.top)
                    rect.top = i;

                if (i > rect.botton)
                    rect.botton = i;
            }
            else {
            }
        }
    }

    int w = rect.right - rect.left;
    int h = rect.botton - rect.top;
    (*buff) = new unsigned char[w * h * bit];
    memset((*buff), 0, w * h * bit);
    stride = width * bit;
    int stride2 = w * bit;
    for (int i = rect.top; i < rect.botton; i++) {
        int widthColorID = i * stride;
        int widthColorID2 = (i - rect.top) * stride2;
        for (int j = rect.left; j < rect.right; j++) {
            unsigned char B = colorBuff[widthColorID + j * bit + 0];
            unsigned char G = colorBuff[widthColorID + j * bit + 1];
            unsigned char R = colorBuff[widthColorID + j * bit + 2];
            unsigned char A = colorBuff[widthColorID + j * bit + 3];


            (*buff)[widthColorID2 + (j - rect.left) * bit + 0] = B;
            (*buff)[widthColorID2 + (j - rect.left) * bit + 1] = G;
            (*buff)[widthColorID2 + (j - rect.left) * bit + 2] = R;
            (*buff)[widthColorID2 + (j - rect.left) * bit + 3] = A;
        }
    }
    if (w > h) {
        unsigned char *tBuff = new unsigned char[w * h * bit];
        //逆时针90度
        {
        }


        //顺时针90度
        {
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    int widthColorID = i * w * bit;

                    unsigned char B = (*buff)[widthColorID + j * bit + 0];
                    unsigned char G = (*buff)[widthColorID + j * bit + 1];
                    unsigned char R = (*buff)[widthColorID + j * bit + 2];
                    unsigned char A = (*buff)[widthColorID + j * bit + 3];

                    int widthColorID2 = j * h * bit;

                    tBuff[widthColorID2 + (h - i - 1) * bit + 0] = B;
                    tBuff[widthColorID2 + (h - i - 1) * bit + 1] = G;
                    tBuff[widthColorID2 + (h - i - 1) * bit + 2] = R;
                    tBuff[widthColorID2 + (h - i - 1) * bit + 3] = A;
                }
            }
            memcpy((*buff), tBuff, w * h * bit);
        }


        delete tBuff;

        rect.left = 0;
        rect.top = 0;
        rect.right = h;
        rect.botton = w;
    }

    if (h > w) {
        int s = 0;
    }

    return rect;
}

void GetColorGropt(unsigned char *colorBuff, int width, int height, int bit) {

    int left = width, top = height, right = 0, botton = 0;
    int stride = width * bit;
    for (int i = 0; i < height; i++) {
        int widthColorID = i * stride;
        for (int j = 0; j < width; j++) {
            unsigned char B = colorBuff[widthColorID + j * bit + 0];
            unsigned char G = colorBuff[widthColorID + j * bit + 1];
            unsigned char R = colorBuff[widthColorID + j * bit + 2];
            if (bit == 4)
                colorBuff[widthColorID + j * bit + 3];

            int C = 100;
            if (B < C && G < C && R < C) {
                //colorBuff[widthColorID + j * bit + 0] = 255;
                //colorBuff[widthColorID + j * bit + 1] = 255;
                //colorBuff[widthColorID + j * bit + 2] = 255;
                colorBuff[widthColorID + j * bit + 3] = 0;
                if (j < left)
                    left = j;

                if (j > right)
                    right = j;

                if (i < top)
                    top = i;

                if (i > botton)
                    botton = i;
            }
            else {
                //colorBuff[widthColorID + j * bit + 0] = 0;
                //colorBuff[widthColorID + j * bit + 1] = 0;
                //colorBuff[widthColorID + j * bit + 2] = 0;
            }
        }
    }

    int w = right - left;
    int h = botton - top;

    if (w > h)
        rotating(colorBuff, width, height, bit, -90);

    int wl = 0;

}

void Linear(unsigned char *colorBuff, int width, int height, int bit) {
    int stride = width * bit;
    for (int i = 2; i < height; i++) {
        int widthColorID1 = (i - 2) * stride;
        int widthColorID2 = i * stride;
        for (int j = 2; j < width; j++) {
            unsigned char B1 = colorBuff[widthColorID1 + (j - 2) * bit + 0];
            unsigned char G1 = colorBuff[widthColorID1 + (j - 2) * bit + 1];
            unsigned char R1 = colorBuff[widthColorID1 + (j - 2) * bit + 2];

            unsigned char B2 = colorBuff[widthColorID2 + (j - 0) * bit + 0];
            unsigned char G2 = colorBuff[widthColorID2 + (j - 0) * bit + 1];
            unsigned char R2 = colorBuff[widthColorID2 + (j - 0) * bit + 2];

            if (fabs(B2 - B1) > 5) {
                colorBuff[widthColorID2 + (j - 0) * bit + 3] = 0;
            }
            if (fabs(G2 - G1) > 5) {
                colorBuff[widthColorID2 + j * bit + 3] = 0;
            }
            if (fabs(R2 - R1) > 5) {
                colorBuff[widthColorID2 + j * bit + 3] = 0;
            }
        }
    }

}

void division(unsigned char *colorBuff, int width, int height, int bit) {
    int stride = width * bit;
    for (int i = 0; i < width; i++) {
        bool isLine = true;
        int widthColorID = i * bit;
        for (int j = 0; j < height; j++) {
            if (colorBuff[widthColorID + j * stride + 3] == 0)
                isLine = false;
            //colorBuff[widthColorID + j * stride + 1] = 255;
            //colorBuff[widthColorID + j * bit + 2] = 200;
        }
        if (isLine) {
            for (int j = 0; j < height; j++) {
                //colorBuff[widthColorID + j * stride + 3] = 0;
            }
        }
        else {
        }
    }

    for (int i = 0; i < height; i++) {
        int widthColorID = i * stride;
        for (int j = 0; j < width; j++) {
            unsigned char B = colorBuff[widthColorID + j * bit + 0];
            unsigned char G = colorBuff[widthColorID + j * bit + 1];
            unsigned char R = colorBuff[widthColorID + j * bit + 2];

            int cc = 30;
            if (fabs(B - G) < cc && fabs(R - G) < cc && B > 20 || B > 200)
                colorBuff[widthColorID + j * bit + 3] = 0;
        }
    }

    for (int i = 0; i < width; i++) {
        int N = 0;
        int H = 0;
        unsigned char A = 0;
        for (int j = 0; j < height; j++) {
            int widthColorID = j * stride;

            unsigned char B = colorBuff[widthColorID + i * bit + 0];
            unsigned char G = colorBuff[widthColorID + i * bit + 1];
            unsigned char R = colorBuff[widthColorID + i * bit + 2];
            if (A != colorBuff[widthColorID + i * bit + 3]) {
                A = colorBuff[widthColorID + i * bit + 3];
                N++;
                if (N == 10)
                    H = j;
            }

        }
    }


}

void rotating(unsigned char *colorBuff, int width, int height, int bit, int r90) {
    unsigned char *buff = new unsigned char[width * height * bit];
    //我们假设要将图像逆时针旋转90度，顺时针是一个道理。如果原图如下所示：
    //	 1  2  3  4
    //	 5  6  7  8
    //	 9 10 11 12
    //	13 14 15 16

    //那么逆时针旋转90度后的图应该是：
    //	4 8 12 16
    //	3 7 11 15
    //	2 6 10 14
    //	1 5  9 13

    //我们要如何原地进行操作以达到上面的效果呢？可以分两步走。 第一步交换主对角线两侧的对称元素，第二步交换第i行和第n - 1 - i行，即得到结果。 看图示：
    //	原图：           第一步操作后：   第二步操作后：
    //	 1  2  3  4         1 5  9 13        4 8 12 16
    //	 5  6  7  8         2 6 10 14        3 7 11 15
    //	 9 10 11 12         3 7 11 15        2 6 10 14
    //	13 14 15 16         4 8 12 16        1 5  9 13

    int stride = width * bit;
    int stride2 = height * bit;
    for (int i = 0; i < height; i++) {
        int widthColorID = i * stride;
        for (int j = 0; j < width; j++) {
            unsigned char B = colorBuff[widthColorID + j * bit + 0];
            unsigned char G = colorBuff[widthColorID + j * bit + 1];
            unsigned char R = colorBuff[widthColorID + j * bit + 2];
            unsigned char A = colorBuff[widthColorID + j * bit + 3];

            int widthColorID2 = j * stride2;

            buff[widthColorID2 + i * bit + 0] = B;
            buff[widthColorID2 + i * bit + 1] = G;
            buff[widthColorID2 + i * bit + 2] = R;
            buff[widthColorID2 + i * bit + 3] = A;
        }
    }
    for (int i = 0; i < width; i++) {
        int widthColorID = i * stride2;
        for (int j = 0; j < height; j++) {
            unsigned char B = buff[widthColorID + j * bit + 0];
            unsigned char G = buff[widthColorID + j * bit + 1];
            unsigned char R = buff[widthColorID + j * bit + 2];
            unsigned char A = buff[widthColorID + j * bit + 3];

            colorBuff[widthColorID + j * bit + 0] = B;
            colorBuff[widthColorID + j * bit + 1] = G;
            colorBuff[widthColorID + j * bit + 2] = R;
            colorBuff[widthColorID + j * bit + 3] = A;
        }
    }
    delete buff;
}

unsigned char color3[3] = {0};

unsigned char *getColor(unsigned char r, unsigned char g, unsigned char b) {
    unsigned char max = r;
    unsigned char min = r;

    if (g > max) max = g;
    if (b > max) max = b;

    if (g < min) min = g;
    if (b < min) min = b;

    float C = max - min;

    ///////////////////////////
    int H = 0;

    if (max == min)
        H = 0;

    if (max == r && g >= b)
        H = (g - b) / C + 0;

    if (max == r && g < b)
        H = (g - b) / C + 360;

    if (max == g)
        H = (b - r) / C + 120;

    if (max == b)
        H = (r - g) / C + 240;

    H = H * 60;

    return color3;
}

void HSV_RGB(float h, float s, float v, unsigned char &R, unsigned char &G, unsigned char &B) {
    int k;
    float aa, bb, cc, f;

    if (s <= 0.0) {
        B = G = B = v * 255;
    }
    else {
        if (h == 1.0) {
            h = 0.0;
        }
        h *= 6.0;
        k = floor(h);
        f = h - k;

        aa = v * (1.0 - s);
        bb = v * (1.0 - s * f);
        cc = v * (1.0 - s * (1.0 - f));

        float r, g, b;

        switch (k) {
            case 0:
                r = v;
                g = cc;
                b = aa;
                break;
            case 1:
                r = bb;
                g = v;
                b = aa;
                break;
            case 2:
                r = aa;
                g = v;
                b = cc;
                break;
            case 3:
                r = aa;
                g = bb;
                b = v;
                break;
            case 4:
                r = cc;
                g = aa;
                b = v;
                break;
            case 5:
                r = v;
                g = aa;
                b = bb;
                break;
        }

        R = r * 255.0;
        G = g * 255.0;
        B = b * 255.0;
    }
}

void RGB_HSV(unsigned char R, unsigned char G, unsigned char B, float &H, float &S, float &V) {
    float minRGB = Min(R, Min(G, B));
    float maxRGB = Max(R, Max(G, B));
    float deltaRGB = maxRGB - minRGB;

    V = maxRGB;

    if (maxRGB != 0) {
        S = deltaRGB / maxRGB;
    }
    else
        S = 0.0;
    if (S < 0.0)
        H = -1.0;
    else {
        if (deltaRGB != 0) {
            if (R == maxRGB)
                H = (G - B) / deltaRGB;
            else if (G == maxRGB)
                H = 2.0 + (B - R) / deltaRGB;
            else if (B == maxRGB)
                H = 4.0 + (R - G) / deltaRGB;
            H *= 60.0;
            if (H < 0.0)
                H += 360;
        }

    }
}




void EdgeDetection(unsigned char *buff, int width, int height, int bit, unsigned char *mask, int lw,
                   int cz) {
    //int lw = 3; int cz = 15;
    for (int i = 5; i < height - 1; i++) {
        //int hid2 = (i - lw-1) * width5 * 4;
        int hid1 = (i - lw) * width * 4;
        int hid = i * width * 4;
        int hid2 = (i + lw) * width * 4;
        for (int j = 5; j < width - 1; j++) {
            int wid2 = (j + lw) * 4;
            int wid1 = (j - lw) * 4;
            int wid = j * 4;

            unsigned char Bh1 = buff[hid1 + wid + 0];
            unsigned char Gh1 = buff[hid1 + wid + 1];
            unsigned char Rh1 = buff[hid1 + wid + 2];
            int maxRGBh1 = Max(Rh1, Max(Gh1, Bh1));
            int minRGBh1 = Min(Rh1, Min(Gh1, Bh1));

            unsigned char Bw1 = buff[hid + wid1 + 0];
            unsigned char Gw1 = buff[hid + wid1 + 1];
            unsigned char Rw1 = buff[hid + wid1 + 2];
            int maxRGBw1 = Max(Rw1, Max(Gw1, Bw1));
            int minRGBw1 = Min(Rw1, Min(Gw1, Bw1));

            unsigned char B = buff[hid + wid + 0];
            unsigned char G = buff[hid + wid + 1];
            unsigned char R = buff[hid + wid + 2];
            int maxRGB = Max(R, Max(G, B));
            int minRGB = Min(R, Min(G, B));

            if (fabs(maxRGB - maxRGBh1) > cz || fabs(minRGB - minRGBh1) > cz) {
                mask[hid + wid + 2] = 255;
            }

            if (fabs(maxRGB - maxRGBw1) > cz || fabs(minRGB - minRGBw1) > cz) {
                {
                    mask[hid + wid + 2] = 255;
                }

            }

        }
    }
}


void EdgeDetectionRGB(unsigned char *buff, int width, int height, int bit, unsigned char *mask,
                      int lw, int cz) {
    for (int i = 5; i < height - 1; i++) {
        int hid1 = (i - lw) * width * 4;
        int hid = i * width * 4;
        for (int j = 5; j < width - 1; j++) {
            int wid = j * 4;

            unsigned char Bh1 = buff[hid1 + wid + 0];
            unsigned char Gh1 = buff[hid1 + wid + 1];
            unsigned char Rh1 = buff[hid1 + wid + 2];

            unsigned char B = buff[hid + wid + 0];
            unsigned char G = buff[hid + wid + 1];
            unsigned char R = buff[hid + wid + 2];

            if (fabs(R - Rh1) > cz || fabs(G - Gh1) > cz || fabs(B - Bh1) > cz) {
                mask[hid + wid + 2] = 255;
            }
        }
    }
}

void EdgeDetectionRGB_C(unsigned char *buff, int width, int height, int bit, unsigned char *mask,
                        int lw, int cz) {
    for (int i = 5; i < height - 1; i++) {
        int hidt = (i - lw) * width * 4;
        int hid = i * width * 4;
        int hidb = (i + lw) * width * 4;
        for (int j = 5; j < width - 1; j++) {
            int widl = (j - 1) * 4;
            int wid = j * 4;
            int widr = (j + 1) * 4;

            unsigned char Bht = buff[hidt + wid + 0];
            unsigned char Ght = buff[hidt + wid + 1];
            unsigned char Rht = buff[hidt + wid + 2];

            unsigned char B = buff[hid + wid + 0];
            unsigned char G = buff[hid + wid + 1];
            unsigned char R = buff[hid + wid + 2];

            unsigned char Bhb = buff[hidb + wid + 0];
            unsigned char Ghb = buff[hidb + wid + 1];
            unsigned char Rhb = buff[hidb + wid + 2];

            unsigned char Bwl = buff[hid + widl + 0];
            unsigned char Gwl = buff[hid + widl + 1];
            unsigned char Rwl = buff[hid + widl + 2];

            unsigned char Bwr = buff[hid + widr + 0];
            unsigned char Gwr = buff[hid + widr + 1];
            unsigned char Rwr = buff[hid + widr + 2];


            if (fabs(Rhb - Rht) > cz || fabs(Ghb - Ght) > cz || fabs(Bhb - Bht) > cz) {
                mask[hid + wid + 2] = 255;
            }
        }
    }
}


void Center_HSV(unsigned char *buff, int width, int height, int bit, int wid, float *h, float *s,
                float *v) {
    for (int i = 0; i < height; i++) {
        int hid = i * width * bit;
        float H, S, V;
        RGB_HSV(buff[hid + wid * bit + 2], buff[hid + wid * bit + 1], buff[hid + wid * bit + 0], H,
                S, V);

        h[i] = H;
        s[i] = S * 255;
        v[i] = V;

        //	buff[hid + wid * bit + 2] = 255;
    }
}


vector<MyColor> colors;

vector<MyColor> color9;
vector<MyColor> *get_color(unsigned char *buff, int width, int height, int bit,
                           unsigned char *outBuff) {
    //vector <MyColor>::iterator Iter;
    for (int i = 0; i < colors.size(); i++) {
        //MYRECT* sg = colors[i];
        colors.erase(colors.begin() + i);
        //delete sg;
    }
    colors.clear();
    for (int i = 0; i < color9.size(); i++)
    {
        color9.erase(color9.begin() + i);
    }
    color9.clear();
    int id = 0;
    int stride = width * 4;
    int i5 = 0;

    int width5 = width / 5 + 1;
    int height5 = height / 5 + 1;
    int bit5 = 4;
    unsigned char *buff5 = new unsigned char[width5 * height5 * bit5];
    memset(buff5, 0, width5 * height5 * bit5);

    //5X5像素合一
    for (int i = 0; i < height; i += 5) {
        int j5 = 0;
        int widthColorID = i * stride;
        for (int j = 0; j < width; j += 5) {
            int rv = 0, gv = 0, bv = 0;
            for (int a = i; a < i + 5; a++) {
                //int hid = a * stride;
                for (int b = j; b < j + 5; b++) {
                    bv += buff[widthColorID + b * bit + 0];
                    gv += buff[widthColorID + b * bit + 1];
                    rv += buff[widthColorID + b * bit + 2];
                }
            }
            rv /= 25;
            gv /= 25;
            bv /= 25;

            int cid = i5 * width5 * 4 + j5 * 4;

            //以RGB色差值存储
            int minRGB = Min(rv, Min(gv, bv));
            int maxRGB = Max(bv, Max(gv, bv));
            int L1 = maxRGB - minRGB;
            buff5[cid + 0] = bv;
            buff5[cid + 1] = gv;
            buff5[cid + 2] = rv;
            buff5[cid + 3] = L1;
            id++;
            j5++;
        }
        i5++;
    }

    unsigned char *wline = new unsigned char[width5];
    memset(wline, 0, width5);
    //扫描线寻找最大高度值
    for (int i = 0; i < height5; i++) {
        int hid = i * width5 * bit5;
        for (int j = 0; j < width5; j++) {
            int wid = j * bit5;
            wline[j] = buff5[hid + wid + 3] > wline[j] ? buff5[hid + wid + 3] : wline[j];
        }
    }

    //查找突出区间
    int start = 0, end = 0;
    for (int i = 1; i < width5; i++) {
        if (wline[i] - wline[i - 1] > 10) {
            int s = i;
            int e = i;
            for (int j = i; j < width5; j++) {
                if (wline[j - 1] >= wline[i] && wline[j] < wline[i]) {
                    e = j;
                    break;
                }
            }
            if (start == 0 && end == 0) {
                start = s;
                end = e;
            }
        }
    }

    unsigned char * hline = new unsigned char[height5];
    memset(hline, 0, height5);
    //扫描线寻找最大高度值
    for (int i = 0; i < height5; i++)
    {
        int hid = i * width5 * bit5;
        for (int j = end +20; j < width5; j++)
        {
            int wid = j * bit5;
            hline[i] = buff5[hid + wid + 3] > hline[i] ? buff5[hid + wid + 3] : hline[i];
        }
    }


    float *H = new float[height5];
    float *S = new float[height5];
    float *V = new float[height5];
    Center_HSV(buff5, width5, height5, bit5, (start + end) / 2, H, S, V);
    unsigned char *isColor = new unsigned char[height5];
    memset(isColor, 0, height5);
    for (int i = 0; i < 360; i++) {
        int wid = i * bit5;
        for (int j = 5; j < height5 - 5; j++) {
            int hid = j * width5 * bit5;

            if (i > S[j]) {
                if (isColor[j] == 0) {
                    for (int k = j - 40; k < j - 10; k++)
                        if (k > 0 && isColor[k] == 0)
                            isColor[k] = 2;

                    for (int k = j - 10; k < j + 10; k++)
                        if (k > 0 && k < height5 && isColor[k] == 0)
                            isColor[k] = 1;

                    for (int k = j + 10; k < j + 40; k++)
                        if (k < height5 && isColor[k] == 0)
                            isColor[k] = 2;
                }

            }

        }
    }

    int lid = 0;
    for (int i = 0; i < height5; i++) {
        int ids = i;
        while (isColor[i] == 1)
            i++;
        if (i > ids)
            lid++;
        if (lid == 6)
            break;
        if (lid > 0) {
            int cid = i;
            while (isColor[i] == 2) {
                i++;
            }
            int ch = (i + cid) / 2;
            if (i > cid) {
                int hid = ch * width5 * bit5;
                MyColor myColor;
                myColor.B = buff5[hid + (start + end) / 2 * 4 + 0];
                myColor.G = buff5[hid + (start + end) / 2 * 4 + 1];
                myColor.R = buff5[hid + (start + end) / 2 * 4 + 2];
                RGB_HSV(myColor.R, myColor.G, myColor.B, myColor.H, myColor.S, myColor.V);
                colors.push_back(myColor);
            }
        }
    }

    delete H;
    delete S;
    delete V;

    for (int i = 0; i < width5; i++)
        wline[i] = wline[i] < 20 ? 0 : 255;
    for (int i = 0; i < height5; i++)
        hline[i] = hline[i] < 20 ? 0 : 255;
    for (int i = 0; i < height5; i++)
    {
        int hsi = i;
        if (hline[i] > 0)
        {
            while (hline[i] > 0)
                i++;

            int hc = (hsi + i) / 2;

            int hid = hc * width5 * bit;

            for (int k = end + 20; k < width5; k++)
            {
                int wsi = k;
                if (wline[k] > 0)
                {
                    while (wline[k] > 0)
                        k++;

                    int wc = (wsi + k) / 2;
                    int wid = wc * bit;

                    MyColor myColor;
                    myColor.B = buff5[hid + wid + 0];
                    myColor.G = buff5[hid + wid + 1];
                    myColor.R = buff5[hid + wid + 2];
                    RGB_HSV(myColor.R, myColor.G, myColor.B, myColor.H, myColor.S, myColor.V);
                    colors.push_back(myColor);

                }
            }
        }

    }
    delete wline;
    delete isColor;
    delete buff5;
    return &colors;
}